package com.jie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.dto.RedisCountDTO;
import com.jie.dto.UserExerciseDTO;
import com.jie.entity.CombinationTopic;
import com.jie.entity.UserExercise;
import com.jie.exception.BizException;
import com.jie.mapper.CombinationTopicMapper;
import com.jie.mapper.UserExerciseMapper;
import com.jie.service.ICombinationTopicService;
import com.jie.service.IUserExerciseService;
import com.jie.utils.BeanCopyUtils;
import com.jie.vo.ConditionVO;
import com.jie.vo.PageResult;
import com.jie.vo.UserExerciseVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户锻炼表 服务实现类
 * </p>
 *
 * @author jie
 * @since 2024-04-20
 */
@Service
public class UserExerciseServiceImpl extends ServiceImpl<UserExerciseMapper, UserExercise> implements IUserExerciseService {

    @Autowired
    private CombinationTopicMapper combinationTopicMapper;
    /**
     *  用户经验值+10 并且当前训练专题人数+1
     * @param topicId
     * @return
     */
    @Override
    public RedisCountDTO addUserExerciseInfo(Integer topicId) {
        //TODO 后续经验增加通过mq来进行消费

        return new RedisCountDTO(topicId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addSportUserExerciseInfo(UserExerciseVO userExerciseVO) {
        if (Objects.isNull(userExerciseVO)){
            throw new BizException("传入对象为空，系统错误");
        }
        UserExercise userExercise = BeanCopyUtils.copyObject(userExerciseVO, UserExercise.class);
        //根据专题id查询并填补
        CombinationTopic combinationTopic = combinationTopicMapper.selectById(userExerciseVO.getTopicId());
        userExercise.setTopicImages(combinationTopic.getTopicImages());
        userExercise.setTopicName(combinationTopic.getTopicName());
        userExercise.setAuthorImages(combinationTopic.getAuthorImages());
        userExercise.setVideoAuthorName(combinationTopic.getVideoAuthorName());
        userExercise.setSportDate(LocalDateTime.now());
        //更新或删除
        return this.saveOrUpdate(userExercise);
        //根据当前专栏id返回对应的url

    }

    @Override
    public List<UserExerciseDTO> getUserExerciseInfoById(Integer userId) {
        //根据userId获取到锻炼信息
        List<UserExercise> userExerciseList = this.list(new LambdaQueryWrapper<UserExercise>()
                .eq(UserExercise::getUserId, userId)
                .orderByDesc(UserExercise::getSportDate)
        );
        return BeanCopyUtils.copyList(userExerciseList, UserExerciseDTO.class);
    }
}
